package com.LeeCode;

import java.util.Arrays;

/**
 * 学生分数的最小差值
 */

public class Code1984 {
    public static void main(String[] args) {
        int[] nums = {9, 4, 1, 7};
        int k = 2;
        System.out.println(new Code1984().minimumDifference(nums, k));
    }

    public int minimumDifference(int[] nums, int k) {
        int ans = Integer.MAX_VALUE;

        Arrays.sort(nums);
        int minVal, maxVal;

        for (int i = k - 1; i < nums.length; i++) {
            minVal = nums[i - k + 1];
            maxVal = nums[i];

            ans = Math.min(ans, maxVal - minVal);
        }
        return ans;
    }
}
